package com.zhaoye.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


import com.zhaoye.common.valid.AddGroup;
import com.zhaoye.common.valid.UpdateGroup;
import com.zhaoye.common.valid.UpdateStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhaoye.gulimall.product.entity.BrandEntity;
import com.zhaoye.gulimall.product.service.BrandService;
import com.zhaoye.common.utils.PageUtils;
import com.zhaoye.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author zhaoye
 * @email 792700497@qq.com
 * @date 2021-11-29 11:09:07
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * JSR303校验
     * 1)统一异常信息处理
     * 1.Bean字段注解,添加自己的提示
     * 2.开启表单校验
     * @Validated 注解是让 普通传参 上的注解搭配使用的
     * @Valid 注解 是让 实体对象传参 上的注解搭配使用的
     * 3.自定义统一异常处理类 @RestControllerAdvice
     * 4.自定义状态码信息
     *  2)分组校验
     *1.添加分组校验
     * 2.@Validated指定分组
     *
     *
     *  3)自定义校验规则
     *  1.编写自定义校验注解
     *  2编写自定义校验器 ConstraintValidator
     *  3.关联二者
     */
    @RequestMapping("/save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand /*BindingResult result*/) {
//        if (result.hasErrors()) {
//            Map<String, String> map = new HashMap<>();
//            //1.获取到校验结果
//            result.getFieldErrors().forEach((item) -> {
//                String message = item.getDefaultMessage();
//                String field = item.getField();
//                map.put(field, message);
//            });
//            return R.error(400, "提交的数据不合法").put("data", map);
//        } else {
//
//        }
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);

        return R.ok();
    }

    @RequestMapping("/update/status")
    public R updateStatus(@Validated(value = {UpdateStatus.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
